package com.giscloud.amp.service.auth.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.giscloud.amp.dao.AmpStaffRoleMapper;
import com.giscloud.amp.entity.AmpStaffRole;
import com.giscloud.amp.entity.AmpStaffRoleExample;
import com.giscloud.amp.entity.dto.AmpStaffRoleDTO;
import com.giscloud.amp.entity.vo.AmpStaffRoleVO;
import com.giscloud.amp.service.auth.AmpStaffRoleService;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author:gxd
 * @Description:权限角色关系操作
 * @Date: 11:04 2018/2/24
 * @Modified By:
 */
@Service("ampStaffRoleService")
public class AmpStaffRoleServiceImpl implements AmpStaffRoleService {

    private final static Logger log = LoggerFactory.getLogger(AmpStaffRoleServiceImpl.class);
    @Autowired
    private AmpStaffRoleMapper ampStaffRoleMapper;

    /**
     * 获取用户的角色列表
     * @param ampStaffRoleDTO
     * @return
     */
    @Override
    public PageInfo getAmpStaffRoleList(AmpStaffRoleDTO ampStaffRoleDTO) {
        log.debug("#getAmpStaffRoleList parameter ={}", JSON.toJSONString(ampStaffRoleDTO));
        PageHelper.startPage(ampStaffRoleDTO.getPage(), ampStaffRoleDTO.getRows());
        AmpStaffRoleExample ampStaffRoleExample = new AmpStaffRoleExample();
        AmpStaffRoleExample.Criteria criteria = ampStaffRoleExample.createCriteria();
        List<AmpStaffRoleVO> ampStaffRoles = ampStaffRoleMapper.selectByExample(ampStaffRoleExample);
        return PageInfo.of(ampStaffRoles);
    }

    /**
     * 通过条件获取用户的角色列表
     * @param ampStaffRoleDTO
     * @return
     */
    @Override
     public PageInfo getAmpStaffRoleListByDTO(AmpStaffRoleDTO ampStaffRoleDTO) {
        log.debug("#getAmpStaffRoleList parameter ={}", JSON.toJSONString(ampStaffRoleDTO));
        PageHelper.startPage(ampStaffRoleDTO.getPage(), ampStaffRoleDTO.getRows());
        List<AmpStaffRoleVO> ampStaffRoles = ampStaffRoleMapper.selectStaffRoleByDTO(ampStaffRoleDTO);
        return PageInfo.of(ampStaffRoles);
    }

    /**
     * 根据条件查询用户角色
     * @param ampStaffRoleDTO
     * @return
     */
    @Override
    public AmpStaffRoleVO getAmpStaffRoleByCondition(AmpStaffRoleDTO ampStaffRoleDTO) {
        log.debug("#getAmpStaffRoleByCondition parameter ={}", JSON.toJSONString(ampStaffRoleDTO));
        AmpStaffRoleExample ampStaffRoleExample = new AmpStaffRoleExample();
        AmpStaffRoleExample.Criteria criteria = ampStaffRoleExample.createCriteria();
        
        if(ampStaffRoleDTO.getRoleId()!= null){
        	criteria.andRoleIdEqualTo(ampStaffRoleDTO.getRoleId());
        }
        if(ampStaffRoleDTO.getStatusCd()!=null){
        	criteria.andStatusCdEqualTo(ampStaffRoleDTO.getStatusCd());
        }
        
        List<AmpStaffRoleVO> ampStaffRoles = ampStaffRoleMapper.selectByExample(ampStaffRoleExample);
        if(!CollectionUtil.isEmpty(ampStaffRoles) && ampStaffRoles.size() == 1){
            return ampStaffRoles.get(0);
        }else{
            return  null;
        }
    }

    /**
     * 插入用户的角色
     * @param ampStaffRoleDTO
     * @return
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int insert(AmpStaffRoleDTO ampStaffRoleDTO) {
        AmpStaffRole ampStaffRole = new AmpStaffRole();
        BeanUtils.copyProperties(ampStaffRoleDTO,ampStaffRole);
        ampStaffRole.setCreatedDt(new Date());
        ampStaffRole.setVersion(new Date());
        return ampStaffRoleMapper.insert(ampStaffRole);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int batchInsert(List<AmpStaffRole> list){
        return ampStaffRoleMapper.batchInsert(list);
    }


    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int batchUpdate(List<AmpStaffRole> list) {
        return ampStaffRoleMapper.batchUpdate(list);
    }


    /**
     * 更新用户的角色状态
     * @param ampStaffRoleDTO
     * @return
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int update(AmpStaffRoleDTO ampStaffRoleDTO) {
        AmpStaffRoleExample ampStaffRoleExample = new AmpStaffRoleExample();
        AmpStaffRoleExample.Criteria criteria = ampStaffRoleExample.createCriteria();
        if(ampStaffRoleDTO.getRelId() != null){
            criteria.andRelIdEqualTo(ampStaffRoleDTO.getRelId());
        }
        AmpStaffRole ampStaffRole = new AmpStaffRole();
        BeanUtils.copyProperties(ampStaffRoleDTO,ampStaffRole);
        ampStaffRole.setCreatedDt(new Date());
        ampStaffRole.setVersion(new Date());
        return ampStaffRoleMapper.updateByExample(ampStaffRole,ampStaffRoleExample);
    }


    /**
     * 删除用户拥有的角色
     * @param relId
     * @return
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int delete(Long relId) {
        AmpStaffRoleExample ampStaffRoleExample = new AmpStaffRoleExample();
        AmpStaffRoleExample.Criteria criteria = ampStaffRoleExample.createCriteria();
        if(relId != null){
            criteria.andRelIdEqualTo(relId);
        }
        return ampStaffRoleMapper.deleteByExample(ampStaffRoleExample);
    }

    /**
     * 根据用户id查询用户拥有的角色编码
     * @param staffId
     * @return
     */
    @Override
    public Set<String> getRoleCodeByCondition(String staffId) {
        Set<String> roleCode = new HashSet<>();
        AmpStaffRoleExample ampStaffRoleExample = new AmpStaffRoleExample();
        AmpStaffRoleExample.Criteria criteria = ampStaffRoleExample.createCriteria();
        criteria.andStatusCdEqualTo((short)1);
        List<AmpStaffRoleVO> ampStaffRoles = ampStaffRoleMapper.selectByExample(ampStaffRoleExample);
        for (AmpStaffRoleVO ampStaffRole : ampStaffRoles) {
            roleCode.add(String.valueOf(ampStaffRole.getRoleId()));
        }
        return roleCode;
    }
}
